package com.monco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.monco.entity.Rubbish;
import com.monco.entity.RubbishRecord;
import com.monco.entity.User;
import com.monco.mapper.RubbishRecordMapper;
import com.monco.service.IRubbishRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.monco.service.IRubbishService;
import com.monco.service.IUserScoreService;
import com.monco.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author com/monco
 * @since 2020-03-11
 */
@Service
public class RubbishRecordServiceImpl extends ServiceImpl<RubbishRecordMapper, RubbishRecord> implements IRubbishRecordService {

    @Resource
    private RubbishRecordMapper rubbishRecordMapper;

    @Resource
    private IUserScoreService userScoreService;

    @Resource
    private IUserService userService;

    @Resource
    private IRubbishService rubbishService;

    @Override
    public List<RubbishRecord> getRubbishRecordList(RubbishRecord rubbishRecord) {
        return rubbishRecordMapper.selectList(getWrapper(rubbishRecord));
    }

    @Override
    public IPage<RubbishRecord> getRubbishRecordPage(IPage<RubbishRecord> page, RubbishRecord rubbishRecord) {
        return rubbishRecordMapper.selectPage(page, getWrapper(rubbishRecord));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRubbishRecord(List<RubbishRecord> rubbishRecordList, Long userId) {
        User user = userService.getById(userId);
        int score = (user.getUserScore() == null) ? 0 : user.getUserScore();
        for (RubbishRecord rubbishRecord : rubbishRecordList) {
            Rubbish rubbish = rubbishService.getById(rubbishRecord.getRubbishId());
            rubbishRecord.setRubbishScore(rubbish.getRubbishScore());
            rubbishRecord.setRubbishType(rubbish.getRubbishType());
            rubbishRecord.setRubbishName(rubbish.getRubbishName());
            rubbishRecord.setTotalScore(rubbishRecord.getRubbishScore() * rubbishRecord.getRubbishAmount());
            rubbishRecord.setNickName(user.getNickName());
            rubbishRecord.setUserId(userId);
            rubbishRecord.setUsername(user.getUsername());
            score = score + rubbishRecord.getTotalScore();
        }
        this.saveBatch(rubbishRecordList);
        user.setUserScore(score);
        userService.updateById(user);
    }

    private QueryWrapper<RubbishRecord> getWrapper(RubbishRecord rubbishRecord) {
        QueryWrapper<RubbishRecord> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(rubbishRecord.getNickName())) {
            queryWrapper.eq("nick_name", rubbishRecord.getNickName());
        }
        if (StringUtils.isNotBlank(rubbishRecord.getRubbishName())) {
            queryWrapper.eq("rubbish_name", rubbishRecord.getRubbishName());
        }
        if (StringUtils.isNotBlank(rubbishRecord.getRubbishType())) {
            queryWrapper.eq("rubbish_type", rubbishRecord.getRubbishType());
        }
        return queryWrapper;
    }
}
